using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Common;


namespace IGlobalist.Cms.Admin.Control
{
    public partial class ClassPropertyAddEdit : AdminBaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {  
            if (!Page.IsPostBack)
            {
                tbName.Enabled = true; 
            }
            
        }

        protected void gvProperty_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            string name = e.CommandArgument.ToString();
            PropertyList propertyList = new PropertyList();
            if (ViewState["propertyList"] != null) propertyList = (PropertyList)ViewState["propertyList"];

            if (e.CommandName == "delProperty")
            {
                foreach (BaseProperty bp in propertyList)
                {
                    if (name == bp.Name)
                    {
                        propertyList.Remove(bp.Name);
                        BindProperty(propertyList);
                        ViewState["propertyList"] = propertyList;
                        tbName.Enabled = true;
                        break;
                    }
                }
            }

            if (e.CommandName == "editProperty")
            {
                foreach (BaseProperty bp in propertyList)
                {
                    if (name == bp.Name)
                    {
                        ViewState["name"] = name;
                        tbDescription.Text = bp.Description;
                        tbDisplay.Text = bp.DisplayName;
                        tbName.Text = bp.Name;
                        tbName.Enabled = false;
                        tbValue.Text = Convert.ToString(bp.Value);
                        ddlInputType.SelectedItem.Text = Convert.ToString(bp.InputType);
                        ddlType.SelectedItem.Text = Convert.ToString(bp.Type);
                       
                        break;
                    }
                }
            }

           
        }

        protected void gvProperty_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                BaseProperty bp = (BaseProperty)e.Row.DataItem;
                if (bp.PropertyType == PropertyType.Main)
                {
                    ((ImageButton)e.Row.FindControl("imgEdit")).Visible=false;
                    ((ImageButton)e.Row.FindControl("ingDelete")).Visible = false;
                }
            }
        }
        

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (tbName.Text.Trim() != "")
            {
                PropertyList propertyList = new PropertyList();
                if (ViewState["propertyList"] != null) propertyList = (PropertyList)ViewState["propertyList"];
                BaseProperty basePro = new BaseProperty();
                basePro.Name = tbName.Text.Replace(" ", "");
                if (IsExist(basePro.DisplayName))
                {
                    basePro.Description = tbDescription.Text;
                    basePro.DisplayName = tbDisplay.Text;
                    basePro.InputType = (InputType)Convert.ToInt16(ddlInputType.SelectedValue);
                    basePro.Type = (DataType)Convert.ToInt16(ddlType.SelectedValue);
                    basePro.PropertyType = PropertyType.Extention;
                    //check default
                    basePro.Value = tbValue.Text;
                    if (DataUtils.IsDataValid((object)basePro.Value, basePro.Type))
                    {
                        if (ViewState["name"] != null)
                        {
                            propertyList.Remove(Convert.ToString(ViewState["name"]));
                            ViewState["name"] = null;
                        }
                        propertyList.Add(basePro);
                        tbName.Enabled = true;
                        ViewState["propertyList"] = propertyList;
                        BindProperty(propertyList);
                        lblError.Text = "";
                        tbDescription.Text = "";
                        tbDisplay.Text = "";
                        tbValue.Text = "";
                        tbName.Text = "";
                    }
                    else
                        lblError.Text = "Value not Valid";
                }
                else
                {
                    lblError.Text = "Name is exist";
                }
            }
            else
            {
                lblError.Text = "Require Name";
            }
        }

        public void BindProperty(PropertyList propertyList)
        {
            ViewState["propertyList"] = propertyList;
            gvProperty.DataSource = propertyList;
            gvProperty.DataBind();
      
        }

        public PropertyList  PropertyList
        {
            get {
                return (PropertyList)ViewState["propertyList"];
            }
        }

        public bool IsExist(string name)
        {
            PropertyList propertyList = new PropertyList();
            if (ViewState["propertyList"] != null) propertyList = (PropertyList)ViewState["propertyList"];
            foreach (BaseProperty bp in propertyList)
            {
                if ((name == bp.Name) || (name == "Type")) return false;
            }

            if (name == "") return false;
            return true;
        }

       
    }
}